// pages/user/presentAccount/presentAccount.js
var app = getApp();
var load= true;
var _PAGESIZE = 10;//默认页量
var _PAGE = 1;//默认页码
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bankList:[],
    workers_id: 0,
    hasMore: false,
    loading: false,
    load_show:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      bankList:[]
    })
    _PAGE = 1;
    this.getbankList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (!that.data.hasMore) {
      return false;
    }
    _PAGE++;
    that.setData({
      loading: true
    })
    that.getbankList();
  },  


  // 添加银行卡
  addAccount: function () {
    wx.navigateTo({
      url: '../addeditAccount/addeditAccount?type=2',
    })
  },

  // 编辑
  eidtcard: function (e) {
    var bank_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../addeditAccount/addeditAccount?type=1&&bank_id=' + bank_id,
    })
  },

  // 删除
  delcard: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var bank_id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否删除该账户',
      success: function (res) {
        if (res.confirm) {
          var bankList = that.data.bankList;         
          app.ajax({
            url: "User/Channel/deleteBank",
            data: {
              bank_id: bank_id
            },
            success: function (res) {
              app.showToast(res.data.message);   
              bankList.splice(index, 1);
              that.setData({
                bankList: bankList
              });        
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 获取银行卡列表
  getbankList(){
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    app.ajax({
      url: 'User/Channel/bankList',
      method: "POST",
      data: {
        user_id: user_id,
        page: _PAGE,
        pageSize: _PAGESIZE,
      },
      success: function (res) {
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              bankList: res.data.data.data,
              empty: false
            })
          } else {
            that.setData({
              bankList: that.data.bankList.concat(res.data.data.data),
              empty: false
            })
          }
          // 是否加载更多
          var hasMore = true;
          if (res.data.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
          load= false;
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
        }
        that.setData({
          load_show:true
        })
      }
    })
  }

})